﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelManager : MonoBehaviour {
	public static LevelManager instance;
	public GameObject leftBasket, rightBasket;
	private List<Vector2> leftBasketPos = new List<Vector2>();
	private List<Vector2> rightBasketPos = new List<Vector2>();
	private float startRate = 0.3f;
	private float endRate = 0.7f;
	private float rateStep = 0;
	private int stepPerUnit = 4;
	public static int basketDirection = -1;


	private void SetBasketPos()
	{
		int totalValue = (int)(4/(endRate-startRate));
		for (int i = 0; i < totalValue; i++) {
			if (rateStep == 0)
				rateStep = startRate;
			else
				rateStep += 0.1f / stepPerUnit;
			Vector2 posLeft =  new Vector2 (Camera.main.ScreenToWorldPoint (Vector2.zero).x, Camera.main.ScreenToWorldPoint (new Vector2( 0, Screen.height *rateStep)).y);
			leftBasketPos.Add (posLeft);
			Vector2 posRight = new Vector2 (Camera.main.ScreenToWorldPoint (new Vector2(Screen.width,0)).x, Camera.main.ScreenToWorldPoint (new Vector2( 0, Screen.height *rateStep)).y);
			rightBasketPos.Add (posRight);
		}
	}
	private void Awake()
	{
		instance = this;
		SetBasketPos ();
		SpawnBasket (false);
  	}

	public static void StartGame()
	{
		instance.SpawnBasket (true);
	}

	public void SpawnBasket(bool start)
	{
		if (!start) {
			leftBasket.SetActive (false);
			rightBasket.SetActive (false);
			return;
		}
		int randomPos = Random.Range (0, leftBasketPos.Count);
		if (basketDirection == 1) {
			basketDirection = -1;
			leftBasket.SetActive (true);
			rightBasket.SetActive (false);
			leftBasket.transform.position = new Vector2 (Camera.main.ScreenToWorldPoint (Vector2.zero).x, leftBasketPos[randomPos].y);
		} else if (basketDirection == -1) {
			basketDirection = 1;
			leftBasket.SetActive (false);
			rightBasket.SetActive (true);
			rightBasket.transform.position = new Vector2 (Camera.main.ScreenToWorldPoint (new Vector2(Screen.width,0)).x, rightBasketPos[randomPos].y);
		}
			
	}
}
